﻿@using System.Reflection
@using JiuLing.CommonLibs.ExtensionMethods
@using JiuLing.CommonLibs.Model

<div class="d-flex flex-column align-center">
    <div class="d-flex align-center">
        <MudText Typo="Typo.overline" Class="cursor-default">
            当前版本
        </MudText>
        <MudText Typo="Typo.caption" Class="ml-1 cursor-default">
            @(_currentVersion)
        </MudText>
    </div>
    <MudButton Variant="Variant.Text"
               Size="Size.Small"
               Color="Color.Inherit"
               Style="opacity: 0.5;"
               Disabled="@_isChecking"
               OnClick="CheckUpdateAsync">

        @if (_isChecking)
        {
            <MudProgressCircular Class="ms-n1" Size="Size.Small" Indeterminate="true" />
            <MudText Class="ms-2">正在检查更新</MudText>
        }
        else
        {
            <MudText>检查更新</MudText>
        }
    </MudButton>
</div>

@code {

    private bool _isChecking = false;
    private string _currentVersion = "";
    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        _currentVersion = Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion.ToString();
    }

    private async Task CheckUpdateAsync()
    {
        _isChecking = true;
        await InvokeAsync(StateHasChanged);

        var app = JiuLing.AutoUpgrade.Shell.AutoUpgradeFactory.Create();
        await app.UseHttpMode("http://api.jiuling.cc/app/check-update/d1te99378038a524cd95a4078886a1eb/windows")
            .RunAsync();
        _isChecking = false;
        await InvokeAsync(StateHasChanged);
    }
}
